package com.lbd.system.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.bean.Authority;
import com.lbd.system.service.AuthorityService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by WangYajie on 2016/9/19.
 */
@RestController
@RequestMapping("/authority")
public class AuthorityController extends BaseController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    AuthorityService authorityService;

    /**
     * 给角色分配权限
     *
     * @param ids 权限ID数组
     * @param rid 角色ID
     * @return
     */
    @RequestMapping(value = "/assign", method = RequestMethod.POST)
    public Result assign(@RequestParam(required = false, name = "mids[]") String[] ids, @RequestParam("rid") String rid) {
        authorityService.assignToRole(ids, rid);
        return new Result(ResultState.SUCCESS, "授权成功");
    }

    /**
     * 给用户分配权限
     *
     * @param ids 权限ID数组
     * @param uid 角色ID
     * @return
     */
    @RequestMapping(value = "/assignToUser", method = RequestMethod.POST)
    public Result assignToUser(@RequestParam(required = false, name = "mids[]") String[] ids, @RequestParam("uid") String uid) {
        authorityService.assignToUser(ids, uid);
        return new Result(ResultState.SUCCESS, "授权成功", "");
    }

    /**
     * 通过角色ID查询所拥有权限
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/listByRole")
    public Result listByRole(String id) {
        List<Authority> list = authorityService.findByRoleId(id);
        return new Result(ResultState.SUCCESS, "成功", list);
    }

    /**
     * 通过用户ID查询所拥有权限
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/listByUser")
    public Result listByUser(String id) {
        List<Authority> list = authorityService.findByEmpId(id);
        return new Result(ResultState.SUCCESS, "成功", list);
    }


}
